<?php


use Laravel\CLI\Tasks\Bundle\Publisher;

class Admin_Listings_Controller extends Base_Controller
{

    public $restful = true;


    public function __construct()
    {
        parent::__construct();
        //--Your Code --//
        $this->theme->set_theme("admin");

    }


    public function get_index()
    {

        $data['posts'] = Post::take(20)->order_by('id', 'DESC')->get();

        $this->theme->title("Business List");
        return $this->theme->render('admin::listing.listings', $data);

    }

    public function get_changestatus($id, $status)
    {

        if ($status == '51') {

            $status = '1';
            $users = Post::
            where('id', '=', $id)
                ->update(array(

                        'status' => $status,

                    )
                );


        } else {
            $status = '51';
            $users = Post::where('id', '=', $id)
                ->update(array(

                        'status' => $status,

                    )
                );


        }

        return Redirect::to("admin/listings");
    }


    public function get_view($pID)
    {


        $data['post'] = Post::where('id', '=', $pID)->first();

        $post = $data['post'];

        if ($post->lat && $post->long) {
            $config = array();
            $long_lat = $post->lat . ',' . $post->long;

            $config['minifyJS'] = TRUE;
            $config['map_type'] = 'ROADMAP';
            $config['trafficOverlay'] = 'TRUE';
            $config['map_height'] = '300px';
            $config['zoom'] = "13";
            $config['center'] = $long_lat;

            $config['adsense'] = FALSE;
            $config['adsensePublisherID'] = Settings::get('en', 'adsense_pub_id');
            $config['adsenseFormat'] = 'HALF_BANNER';
            $config['adsensePosition'] = 'BOTTOM_LEFT';

            $Gmap = new Gmaps();
            $Gmap->initialize($config);
            $marker = array();
            $marker['icon'] = \Laravel\URL::base() . "/public/pointer1.png";
            $marker['position'] = $long_lat;
            $marker['infowindow_content'] = "<strong>" . $post->title . "</strong>" . "</br>" . $post->address1;
            $marker['animation'] = 'DROP';
            $Gmap->add_marker($marker);

            $data['map'] = $Gmap->create_map();
        }
        $this->theme->title("View Listing");
        return $this->theme->render('admin::listing.view', $data);
    }

    public function get_delete($id)
    {

        Post::where('id', '=', $id)->delete();

        return Redirect::to("admin/user");
    }

    public function get_edit($id)
    {

        $post = Post::where('id', '=' . $id)->get();

        dd($post);

    }



    public function get_plan($post_id=0){
        $data['post'] = Post::findPK($post_id);
        $data['plans'] = DB::table('membership')->get();

        //--Check Existing Plan --//
        $plan = DB::table('membership')
            ->join('featured','membership.id', '=', 'featured.plan')
            ->where('featured.post_id', '=', $post_id)
            ->first();

        if(isset($plan) && !empty($plan)){
            $data['existing_plan'] = $plan->name;
        }else{
            $data['existing_plan'] = 'Free Plan';
        }

        $this->theme->title("Business Plan");
        return $this->theme->render('admin::listing.plan', $data);
    }

    public function post_plan(){

        $rules = array(
            'post_id' => 'required',
            'plan' => 'required',
        );

        $validator = Validator::make(Input::get(), $rules);
        if( $validator->fails() )
        {
            return Redirect::back()->with_errors($validator)->with('message','<div class="alert alert-danger">Invalid Operation !</div>');

        }else{
            $post_id = Input::get('post_id');
            $plan = DB::table('membership')->where_id(Input::get('plan'))->first();
            $days = $plan->period;
            $exp_date = date('Y-m-d', strtotime("+ $days days"));

            $post_data = array(
                'post_id' => $post_id,
                'plan' => Input::get('plan'),
                'valid_upto' => $exp_date
            );

            DB::query("delete from featured where post_id = $post_id");
            if(Input::get('plan') != 1){
                DB::table('featured')->insert($post_data);
            }
            return Redirect::back()->with_errors($validator)->with('message','<div class="alert alert-danger">Successfull !</div>');
        }

        dd($plan);
    }

}